package cn.bqjr.eily.samples.controller.common;

import cn.bqjr.eily.mybatis.spring.boot.utils.PagingUtils;
import cn.bqjr.eily.samples.controller.base.BaseController;
import cn.bqjr.eily.samples.entity.po.Role;
import cn.bqjr.eily.samples.entity.vo.RoleVO;
import cn.bqjr.eily.samples.facade.IRoleFacade;
import cn.bqjr.eily.samples.service.base.IMenuService;
import cn.bqjr.eily.samples.service.base.IRoleService;
import cn.bqjr.eily.utils.I18nUtils;
import cn.bqjr.eily.web.modules.apiversion.ApiVersion;
import cn.bqjr.eily.web.utils.AssertUtils;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/{version}/roles")
@ApiVersion(1)
public class RoleController extends BaseController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IRoleFacade roleFacade;

    @Autowired
    private IMenuService menuService;

    /**
     * 新增角色
     * @param roleVO
     * @return
     */
    @PostMapping
    public Object addRole(@RequestBody RoleVO roleVO) {
        return roleFacade.addRoleVO(roleVO);
    }

    /**
     * 删除角色
     * @param roleId
     * @return
     */
    @DeleteMapping(value = "/{roleId}")
    public Object deleteRole(@PathVariable("roleId") Long roleId) {
        return roleFacade.deleteRole(roleId);
    }

    /**
     * 批量删除角色
     * @param roleIds
     * @return
     */
    @DeleteMapping
    public Object deleteRoleByBatchId(@RequestParam("ids") List<Long> roleIds) {
        return roleFacade.deleteRoleByBatchId(roleIds);
    }

    /**
     * 更新角色
     * @param roleId
     * @param roleVO
     * @return
     */
    @PutMapping(value = "/{roleId}")
    public Object updateRole(@PathVariable("roleId") Long roleId,
                             @RequestBody RoleVO roleVO) {
        AssertUtils.equals(roleId, roleVO.getRoleId());
        AssertUtils.notNull(roleService.selectById(roleId),
                I18nUtils.getMsg("error.request.inValidRecord"));
        return roleFacade.updateRoleVO(roleVO);
    }

    /**
     * 查询指定角色信息
     * @param roleId
     * @return
     */
    @GetMapping(value = "/{roleId}")
    public Object selectRole(@PathVariable("roleId") Long roleId) {
        return roleService.selectById(roleId);
    }

    /**
     * 查询角色列表
     * @return
     */
    @GetMapping()
    public Object selectRoleList() {
        PagingUtils.pagingIfExist(request);
        return roleService.selectList(new EntityWrapper<Role>());
    }

    /**
     * 查询指定角色的菜单列表
     * @param roleId
     * @return
     */
    @GetMapping(value = "/{roleId}/menus")
    public Object selectRoleMenus(@PathVariable("roleId") Long roleId) {
        PagingUtils.pagingIfExist(request);
        return menuService.selectMenuIdsByRole(roleId);
    }
}
